package com.wangyonghao.harmonyocrs.slice;

import com.wangyonghao.harmonyocrs.MainAbility;
import com.wangyonghao.harmonyocrs.ResourceTable;
import com.wangyonghao.harmonyocrs.dao.UserDao;
import com.wangyonghao.harmonyocrs.pojo.User;
import com.wangyonghao.harmonyocrs.utils.HttpUtils;
import com.wangyonghao.harmonyocrs.utils.StringUtils;
import com.wangyonghao.harmonyocrs.utils.ToastUtils;
import com.wangyonghao.harmonyocrs.utils.UserHolder;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;
import ohos.agp.window.service.WindowManager;
import ohos.data.DatabaseHelper;

import java.util.HashMap;
import java.util.Map;

public class LoginAbilitySlice extends AbilitySlice {
    TextField account;
    TextField password;
    Button login;
    Button regist;
    UserDao userDao;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
        userDao = new UserDao(new DatabaseHelper(this));
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN |
                WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS |
                WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION);
        initComponent();
        event();
    }

    private void event() {
        login.setClickedListener(e -> {
            String accountText = account.getText().toString();
            String passwordText = password.getText().toString();
            if (StringUtils.isEmpty(accountText) || StringUtils.isEmpty(passwordText)) {
                ToastUtils.showDialog(this, "请输入用户名和密码！");
                return;
            }
            User user = userDao.login(accountText, passwordText);
            if (user == null) {
                ToastUtils.showDialog(this, "用户名或密码错误！");
                return;
            }
            ToastUtils.showDialog(this, "欢迎您："+user.getName()+"！");
            UserHolder.setUser(user);
            Intent i = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(getBundleName())
                    .withAbilityName(MainAbility.class.getName())
                    .build();
            i.setOperation(operation);
            startAbility(i);
        });
        regist.setClickedListener(e -> {
            Intent i = new Intent();
            present(new RegistAbilitySlice(), i);
        });
    }

    private void initComponent() {
        account = (TextField) findComponentById(ResourceTable.Id_login_account_input);
        password = (TextField) findComponentById(ResourceTable.Id_login_password_input);
        login = (Button) findComponentById(ResourceTable.Id_login_login_button);
        regist = (Button) findComponentById(ResourceTable.Id_login_regist_button);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
